package com.dingding.sync.ding.service;

import com.dingding.sync.repository.entity.Goods;
import com.dingding.sync.repository.mapper.GoodsMapper;
import com.dingding.sync.repository.request.GoodsRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author: zhangyanan
 * @Date: 2020-05-03 22:07
 * @Description:
 */
@Service
@Slf4j
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 获取用户信息
     *
     *
     */
    public List<Goods> getGoodsList(String title){
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("title", "%" + title + "%");
        List<Goods> list = goodsMapper.selectByExample(example);
        return list;
    }

    /**
     * 更新商品
     *
     */
    public void updateGoods(GoodsRequest request) {

        Goods goods = new Goods();
        BeanUtils.copyProperties(request,goods);
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",request.getId());
        goodsMapper.updateByExampleSelective(goods,example);
    }

    /**
     * 添加商品
     *
     */
    public void addGoods(GoodsRequest request) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(request,goods);
        goods.setId(null);
        goodsMapper.insertSelective(goods);

    }
    /**
     * 删除商品
     *
     *
     */
    public void deleteGoods(Integer id) {
        goodsMapper.deleteByPrimaryKey(id);
    }
}
